package leetcode_day._2021._202111;

import java.util.*;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/11/24 10:54
 * 从英文中重建数字
 * 给你一个字符串 s ，其中包含字母顺序打乱的用英文单词表示的若干数字（0-9）。按 升序 返回原始的数字。
 * 算法：字符串 + 模拟 + 脑筋急转弯
 * 根据数字的英文单词，找到每个英文单词特有的字符，方便排序
 * 字母	数字
 * e	0 1 3 5 7 8 9
 * f	4 5
 * g	8
 * h	3 8
 * i	5 6 8 9
 * n	1 7 9
 * o	0 1 2 4
 * r	0 3 4
 * s	6 7
 * t	2 3 8
 * u	4
 * v	5 7
 * w	2
 * x	6
 * z	0
 * 这样就可以得出遍历顺序
 * 0 2 4 6 8 3 5 7 9 1
 */
public class _24_423 {
    public static void main(String[] args) {
        countWord();
    }

    static void countWord() {
        String[] dic = {"zero", "one", "tow", "three", "four", "five", "six", "seven", "eight", "nine"};
        Map<Character, Set<Integer>> map = new HashMap<>();
        int idx = 0;
        for (String s : dic) {
            for (char c : s.toCharArray()) {
                final Set<Integer> set = map.getOrDefault(c, new HashSet<>());
                set.add(idx);
                map.put(c, set);
            }
            ++idx;
        }
        System.out.println(map);
    }

    public static String originalDigits(String s) {
        String[] dic = {"zero", "one", "tow", "three", "four", "five", "six", "seven", "eight", "nine"};
        int[] priority = {0, 2, 4, 6, 8, 3, 5, 7, 9, 1};
        int[] cnts = new int[26];
        for (int i = 0; i < s.length(); i++) cnts[s.charAt(i) - 'a']++;
        StringBuilder sb = new StringBuilder();
        for (int idx : priority) {
            int max = Integer.MAX_VALUE;
            for (char c : dic[idx].toCharArray()) max = Math.min(max, cnts[c - 'a']);
            for (char c : dic[idx].toCharArray()) cnts[c - 'a'] -= max;
            while (max-- > 0) sb.append(idx);
        }
        final char[] cs = sb.toString().toCharArray();
        Arrays.sort(cs);
        return String.valueOf(cs);
    }

}
